package com.gexiaobao.pigeon.ui.fragment.mine.organization.detail

import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.model.bean.OrgInfoDetailResponse
import com.gexiaobao.pigeon.databinding.FragmentIntrodutionBinding
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.ui.fragment.mine.organization.activity.ActivityAddCarInfo
import com.gexiaobao.pigeon.ui.fragment.mine.organization.activity.ActivityApplyPigeonCarrier
import com.gexiaobao.pigeon.viewmodel.OrgDetailViewModel
import com.gexiaobao.pigeon.viewmodel.SharedViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 16:10
 * @Describe    : 我的组织-简介
 */
class FragmentIntroduction : BaseFragment<OrgDetailViewModel, FragmentIntrodutionBinding>() {

    private var carState = false
    private var mDesc: String = ""
    private var mOrgId: Int = 0

    private var viewModel: SharedViewModel? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProvider(requireActivity())[SharedViewModel::class.java]
        upDateUiState(carState)
    }

    /** 接收子fragment传递过来的数据  */
    fun receiveData(data: OrgInfoDetailResponse) {
        mDesc = data.desc
        carState = data.isTransport
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.tvContentDescribe.movementMethod = ScrollingMovementMethod.getInstance()
        mOrgId = requireArguments().getInt("orgId")
        mDatabind.tvRequestPigeonCarrier.isVisible = true

        eventViewModel.toRefreshDataStr.observeInFragment(this) {
            if (it == "ActivityAddCarInfo") {
                mViewModel.orgInfoToApp(mOrgId.toString())
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.deleteCatcherResult.observe(this) {
            mViewModel.orgInfoToApp(mOrgId.toString())
        }
        /** 获取公棚详情信息 */
        mViewModel.orgInfoDetailResult.observe(viewLifecycleOwner) {
            if (it != null) {
                carState = it.isTransport
                upDateUiState(carState)
            }
        }

        /** 查询接鸽员申请中关于个人的信息查询（用户角色）  */
        mViewModel.drippingBaseInfoResult.observe(viewLifecycleOwner) {
            if (it != null) {
                if (it.id == 0) {
                    startActivity<ActivityApplyPigeonCarrier>("orgId" to mOrgId)
                } else {
                    startActivity<ActivityAddCarInfo>("orgId" to mOrgId, "baseInfoId" to it.id, "type" to "FragmentIntroduction")
                }
            }
        }
    }

    private fun upDateUiState(carState: Boolean) {
        if (carState) {
            mDatabind.tvRequestPigeonCarrier.text = "取消接鸽员"
            mDatabind.tvRequestPigeonCarrier.background = ContextCompat.getDrawable(requireContext(), R.drawable.round_btn_99ccff_radius5_bg)
        } else {
            mDatabind.tvRequestPigeonCarrier.text = "申请接鸽员"
            mDatabind.tvRequestPigeonCarrier.background = ContextCompat.getDrawable(requireContext(), R.drawable.round_btn_99ccff_radius5_bg)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvRequestPigeonCarrier) {
            when (it) {
                mDatabind.tvRequestPigeonCarrier -> {
                    if (carState) {
                        showDialogDeleteTransporter()
                    } else {
                        showLoading(getString(R.string.loading))
                        mViewModel.getTransporterApplyBaseByUserId()

                    }
                }
            }
        }
    }

    private fun showDialogDeleteTransporter() {
        val dialog = RxDialogDefault(context)
        dialog.setContent("取消接鸽员身份后将不可以接单，确认取消？")
        dialog.setSureListener {
            showLoading(getString(R.string.loading))
            mViewModel.deleteTransporter(mOrgId.toString())
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    override fun onResume() {
        super.onResume()
        mViewModel.desc.set(mDesc)
    }

    companion object {

        fun newInstance(orgId: Int): FragmentIntroduction {
            val fragment = FragmentIntroduction()
            val bundle = Bundle()
            bundle.putInt("orgId", orgId)
            fragment.arguments = bundle
            return fragment
        }
    }
}